<?php
namespace backend\controllers;

use backend\models\Admin;
use backend\models\AdminSearch;
use Yii;
use yii\base\Exception;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;
use yii\web\HttpException;

/**
 * Site controller
 */
class AdminController extends BaseValidationController
{
    //public $layout = 'main1.php';
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'delete', 'view'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }


    /**
     * 列表
     * @return string
     */
    public function actionIndex()
    {
        $searchModel = new AdminSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['dataProvider' => $dataProvider, 'searchModel' => $searchModel]);
    }


    /**
     * 添加
     */
    public function actionCreate()
    {
        $model = new Admin(['scenario' => 'add']);
        //验证管理员账号是否已添加

        if ($model->load(Yii::$app->request->post())) {
            $transaction = Yii::$app->db->beginTransaction();
            try {
                $model->password = strtoupper(md5($model->password));
                if (!$model->save()) {
                    throw new Exception;
                }
                $user_id = $model->admin_id;
                $authManager = Yii::$app->authManager;
                $roleName = $authManager->getRole($model->role);
                $authManager->assign($roleName, $user_id);

                $transaction->commit();
                Yii::$app->session->setFlash('success', '添加管理员成功');
                return $this->redirect('create');
            } catch (Exception $e) {
                $transaction->rollBack();
                Yii::$app->session->setFlash('danger', '添加管理员失败');
//                return $this->showResult('400', '失败');
            }
        } else {
            return $this->render('create', ['model' => $model]);
        }
    }

    /**
     * 修改
     */
    public function actionUpdate()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->loadModel($id);
        if ($model->load(Yii::$app->request->post())) {
            $transaction = Yii::$app->db->beginTransaction();
            try {
                if (!$model->save()) {
                    Yii::$app->session->setFlash('danger', '修改管理员失败');
                }
                $transaction->commit();
                Yii::$app->session->setFlash('success', '修改管理员成功');
            } catch (Exception $e) {
                $transaction->rollBack();
                Yii::$app->session->setFlash('danger', '修改管理员失败');
            }
            return $this->redirect('index');
        } else {
            return $this->render('update', ['model' => $model]);
        }
    }

    /**
     * 详情
     */
    public function actionView()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->loadModel($id);
        return $this->render('view', ['model' => $model]);
    }

    public function actionDelete()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->loadModel($id);
        $model->is_del = 1;
        if ($model->save()) {
            Yii::$app->session->setFlash('danger', '删除广告成功');
        } else {
            Yii::$app->session->setFlash('danger', '删除广告失败');
        }
        return $this->redirect('index');
    }

    protected function loadModel($id)
    {
        if ($id) {
            $model = Admin::findOne($id);
            return $model;
        }
        throw new HttpException(404);
    }
}
